/**
 * Created by chengming on 18/4/13.
 * version 1.0
 */
"use strict";
const schedule = require('node-schedule');
const logger = require('./conf/logger');
const request = require('request-promise');
const ip = require('./util/ip');
const sleep = require('await-sleep');
const report = require('./exception/ErrorReport');
const server = require('./conf/server');
const iconv = require("iconv-lite");

const ArticleHrefAdapter = require('./adapter/ArticleHrefAdapter');
const SpecialAdapter = require('./adapter/SpecialAdapter');
const JsonAdapter = require('./adapter/JsonAdapter');
const JsonpAdapter =require('./adapter/JsonpAdapter');
const type = require('./conf/argv')();
const socket = require('./socket/client').socketClient;

/*
await new SpecialAdapter({
	"pageSite": "http://money.163.com/",
	"realSite": "http://money.163.com/special/00251G8F/news_json.js",
	"pageTitle": "网易财经",
	"deltaTime": "30000",
	"followLink": 0,
	"titleRule": "",
	"contentRule": "",
	"foreign": 0,
	"linkRule": "list#title#url",
	"headers": '',
	"cates": "加密货币",
	"priority": "50",
	"linkUniqueBy": 1,
	"encoding": "gbk",
	"ctype": 2,
	"channel": '公告'
}, false).execute();
*/

(async function(){

    socket.connect();

    while(true){

		let echo;
		try{
			echo = await request({
				url: server.addr+'/seed/'+type,
				json:true
			});

			if(echo.code != 200){
				//logger.warn('队列中没有待处理任务');
				await sleep(1000);
			}else{
				if(echo.data) {
                    logger.debug("从种子队列中获取任务:" + JSON.stringify(echo));
                    let page = echo.data;
                    if (1 == page.asSpecial || SpecialAdapter.specialSites.indexOf(page.pageSite.trim()) != -1) {
                        //处理特殊情况
                        await new SpecialAdapter(page, false).execute();
                    } else if (1 == page.ctype) {
                        //内容为普通html
                        await new ArticleHrefAdapter(page, false).execute();
                    } else if (2 == page.ctype) {
                        //内容为json
                        await new JsonAdapter(page, false).execute();
                    } else if (3 == page.ctype) {
                        //内容为jsonp
                        await new JsonpAdapter(page, false).execute();
                    }
                }
			}
		}catch(e){
            logger.error(e);
            logger.error('在处理seed->'+JSON.stringify(echo)+'出错了,5秒后重试');
            if(echo && echo.code == 200){
                if(e.message.indexOf('"type":"Buffer"')>-1) {
                    report(echo.data.pageSite, echo.data.pageTitle,'', '获取种子站点异常,异常原因:'+e.message+ "\n error: " + iconv.decode(e.error, 'utf-8').toString() + " \n错误堆栈:"+e.stack);
                } else {
                    report(echo.data.pageSite, echo.data.pageTitle,'', '获取种子站点异常,异常原因:'+e.message+" \n错误堆栈:"+e.stack);
                }
            }
            await sleep(5000);
		}
	}

	socket.disconnect();

})();
